package org.study.plus.spring.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.*;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.study.plus.spring.mapper.StudentMapper;
import org.study.plus.spring.model.Student;

import java.util.List;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-05-24
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Override
    public int insert(Student student) {
        return studentMapper.insert(student);
    }

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectList(null);
    }

    @Override
    public com.baomidou.mybatisplus.extension.plugins.pagination.Page<Student> selectPage(Student student, int pageNum, int pageSize) {
        // 构建条件
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Student> pageParam = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize);
        // 分页条件查询
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Student> resultPage = this.lambdaQuery()
                .like(StringUtils.isNotBlank(student.getName()), Student::getName, student.getName())
                .eq(student.getId() != null, Student::getId, student.getId())
                // .ge(student.getCreateTime() != null, Student::getCreateTime, student.getCreateTime())
                // .le(student.getCreateTime() != null, Student::getCreateTime, student.getCreateTime())
                //.between(Student::getAge, 18, 60)
                // 按创建时间排序
                // .orderByDesc(Student::getCreateTime)
                .page(pageParam);
        return resultPage;
    }

    @Override
    public PageInfo<Student> startPage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentMapper.selectList(null);
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(studentList);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> startPage2(int pageNum, int pageSize) {
        Page<Student> startPage = PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentMapper.selectList(null);
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(startPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage1(int pageNum, int pageSize) {
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> studentList = studentMapper.selectList(null);
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(studentList);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage2(int pageNum, int pageSize) {
        Page<Student> studentPage = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> studentList = studentMapper.selectList(null);
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(studentPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageJdk7(int pageNum, int pageSize) {
        Page<Student> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(new ISelect() {
                    @Override
                    public void doSelect() {
                        studentMapper.selectList(null);
                    }
                });
        PageInfo<Student> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageJdk8(int pageNum, int pageSize) {
        Page<Student> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> studentMapper.selectList(null));
        PageInfo<Student> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageInfo(int pageNum, int pageSize) {
        PageInfo<Student> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> studentMapper.selectList(null));
        return pageInfo;
    }

    @Override
    public long countJdk7() {
        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                studentMapper.selectList(null);
            }
        });
        return count;
    }

    @Override
    public long countJdk8() {
        long count = PageHelper.count(() -> studentMapper.selectList(null));
        return count;
    }
}
